/**
 * 
 */
package com.goldsign.pay.provider.sample.configuration;


import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

//import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
//import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;


/**
 * @author johnny
 *
 */
@Configuration
@EnableConfigurationProperties
@PropertySource("classpath:application.yml")
public class DruidDataSourceConfig {
	@Value("${type}")
    private Class<? extends DataSource> dataSourceType;
	
	@Primary
	@Bean(name="devDataSource")
    @ConfigurationProperties(prefix="spring.datasource") 
    public DruidDataSource primaryDataSource(){
        System.out.println("-------------------- DataSource init ---------------------");
        DruidDataSource ds = (DruidDataSource) DataSourceBuilder.create().type(dataSourceType).build();
        try {
			ds.setFilters("stat,wall");
		} catch (SQLException e) {
			e.printStackTrace();
		}  
        return ds;
    }

//    @Bean(name="productDataSource")
//    @ConfigurationProperties(prefix="spring.datasource.product")
//    public DataSource secondaryDataSource(){
//        System.out.println("-------------------- productDataSource init ---------------------");
//        return new DruidDataSource();
//    }
	
	/**
	 * 注册一个StatViewServlet
	 * @return
	 */
	@Bean  
	@SuppressWarnings({ "unchecked", "rawtypes" })
    public ServletRegistrationBean druidServlet() {  
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();  
        servletRegistrationBean.setServlet(new StatViewServlet());  
        servletRegistrationBean.addUrlMappings("/druid/*");  
        
        //添加初始化参数：initParams
        Map<String, String> initParameters = new HashMap<String, String>();  
        //用户名  
        initParameters.put("loginUsername", "druid");
        //密码  
        initParameters.put("loginPassword", "druid");
        
        //IP白名单 (没有配置或者为空，则允许所有访问)  
        initParameters.put("allow", "127.0.0.1");
        
        //IP黑名单 (存在共同时，deny优先于allow) : 如果满足deny的话提示:Sorry, you are not permitted to view this
        initParameters.put("deny", "192.168.20.38");

        //是否能够重置数据
        initParameters.put("resetEnable","true"); 
        
        servletRegistrationBean.setInitParameters(initParameters);  
        
        return servletRegistrationBean;  
    }  
  
	@Bean  
	@SuppressWarnings({ "rawtypes", "unchecked" })
    public FilterRegistrationBean filterRegistrationBean() {  
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();  
        filterRegistrationBean.setFilter(new WebStatFilter());  
        filterRegistrationBean.addUrlPatterns("/*");  
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");  
        return filterRegistrationBean;  
    }  
  
/*    // 按照BeanId来拦截配置 用来bean的监控  
    @Bean(value = "druid-stat-interceptor")  
    public DruidStatInterceptor DruidStatInterceptor() {  
        DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();  
        return druidStatInterceptor;  
    }  
  
    @Bean  
    public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {  
        BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();  
        beanNameAutoProxyCreator.setProxyTargetClass(true);  
        // 设置要监控的bean的id  
        //beanNameAutoProxyCreator.setBeanNames("sysRoleMapper","loginController");  
        beanNameAutoProxyCreator.setInterceptorNames("druid-stat-interceptor");  
        return beanNameAutoProxyCreator;  
    } */
}
